<template>
  <div ref="serviceTabRef" style="padding:20px">
    <div class="tips">
      【说明】：PMS“乙供物资明细编制”功能是项目经理针对项目中使用的总包服务（施工总承包、EPC总承包）、外电引入施工服务、砖混/框架机房施工服务确定服务内主设备物资的数量与金额，
      以便资源与财务系统明确该服务具体做了哪些工作，是否需要进行装配、转资，如何装配、转资及形成什么类型的资源、资产主设备信息。
    </div>
    <el-tabs v-model="activeName" type="border-card" @tab-click="getList">
      <el-tab-pane v-for="item in tabsList" :key="item.name" :label="item.label" :name="item.name">
        <el-table ref="table" :data="tableData" border>
          <el-table-column v-if="urlParams.ifSingleProject==1" align="center" prop="prjCode" label="单项工程编码" width="200" />
          <el-table-column align="center" prop="siteId" label="站址编码" width="200" />
          <el-table-column align="center" prop="pmsServiceCode" label="物料编码" width="200" />
          <!-- 新需求添加是否组合件 -->
          <el-table-column align="center" label="是否组合件" width="100">
            <template slot-scope="scope">
              {{ scope.row.ifMultiName }}
            </template>
          </el-table-column>
          <el-table-column align="center" prop="pmsServiceName" label="物料名称" show-overflow-tooltip />
          <el-table-column align="center" prop="sumMoney" width="150" label="累计验工计价金额" />
          <el-table-column align="center" prop="sumProportion" width="150" label="明细占比总和(%)" />
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope">
              <el-button
                v-if="urlParams.ifEdit === '1'"
                class="pms-button-primary operate-btn"
                @click="handleConfiguration(scope.row, true)"
              >
                配置明细
              </el-button>
              <el-button class="pms-button-primary operate-btn" @click="handleConfiguration(scope.row, false)">
                查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <ServiceConfigDetails ref="details" @refreshTable="refreshTable" />
    </el-tabs>
    <div style="display:flex;justify-content: center;margin-top:10px">
      <el-button
        v-if="urlParams.ifEdit !== '0'"
        type="primary"
        class="pms-button-primary"
        @click="handleSend"
      >确认</el-button>
    </div>
    <DialogConfirm v-if="dialogVisible2" :message="[dialogMessage]" @changeStatus="changeMsg" />
  </div>
</template>

  <script>
  import ServiceConfigDetails from './ServiceConfigDetails'
  import DialogConfirm from '@/views/acceptanceManagement/materialServiceBind/components/dialogConfirm.vue'
  import {
    servDetailPrjsCheck,
    initServiceDetailDataNew,
    getSiteService,
    getSumMoneyIsNullNew,
    saveDetailVerifyNew,
    deleteAllUselessDetailNew
  } from '@/api/acceptanceManagement'
  import { parseParams } from '@/utils'

  export default {
    name: 'ServiceTabs',
    components: {
      ServiceConfigDetails,
      DialogConfirm
    },
    data() {
      return {
        activeName: '1',
        tableData: [],
        tabsList: [
          { label: '总包服务', name: '1' },
          { label: '外电引入施工服务', name: '2' },
          { label: '砖混/框架机房服务', name: '3' }
        ],
        urlParams: {
          prjId: null,
          subPrjId: null,
          ifEdit: '1',
          type: null,
          isWhite: false,
          ifSingleProject: ''
        },
        row: {},
        dialogMessage: '项目下存在累计验工计价为0但已配置了明细的服务，请确认是否删除明细？确认后在PC端添加的服务明细将自动删除？, （使用APP贴签赋码添加的明细请在APP端操作删除）。如不删除明细需对该服务做验工计价！',
        dialogVisible2: false
      }
    },
    async created() {
      // this.urlParams.type : 4 （销项）
      this.urlParams = {
        ...this.urlParams,
        ...parseParams(window.location.hash)
      }

      this.handleQuery()
    },

    methods: {
      refreshTable(val) {
        this.handleQuery(val)
    },
      shoService(data) {
        this.urlParams.prjId = data.prjId
        this.urlParams.subPrjId = data.subPrjId
        this.urlParams.ifSingleProject = data.ifSingleProject
        this.urlParams.type = data.type
        this.urlParams.ifEdit = data.ifEdit
      },
      async handleQuery(val = '1') {
        this.urlParams.subPrjIds = this.urlParams.ifSingleProject === '0' ? null : this.urlParams.subPrjId.split(',')
        const { data } = await servDetailPrjsCheck(this.urlParams.ifSingleProject === '0' ? [this.urlParams.prjId] : this.urlParams.subPrjIds)
        this.urlParams.projectPilots = data
        const dataObj = {
          prjId: this.urlParams.prjId,
          ifSingleProject: this.urlParams.ifSingleProject,
          projectPilots: data
        }
        initServiceDetailDataNew(dataObj)
        this.$nextTick(() => {
          this.getList({ name: val })
        })
      },
      // 配置明细 or 查看
      handleConfiguration(row, editable) {
        this.urlParams.isPilot = this.urlParams.projectPilots.find(item => item.prjId === row.subPrjId).isPilot
        if (+this.urlParams.type !== 4 && row.ifHave === '1' && editable) {
          return this.$message.error('此站址服务下存在未推送或项目进度不为100%的验工计价')
        }
        if (+row.ifHave === 0 && +row.sumMoney === 0 && editable) {
          return this.$message.error(`此站址【${row.siteId}】下的服务【${row.pmsServiceCode}】验工计价已全部损失,无需配置明细`)
        }
        this.$refs['details'].getRow({ ...row, type: this.activeName, editable, urlParams: this.urlParams, typeIndex: this.activeName })
      },
      getList(val) {
        const obj = {
          prjId: this.urlParams.prjId,
          ifSingleProject: this.urlParams.ifSingleProject,
          subPrjIds: this.urlParams.subPrjIds,
          type: val.name
        }
        getSiteService(obj).then(res => {
          this.tableData = res.data
          this.$nextTick(() => {
            const h = this.$refs.serviceTabRef.clientHeight
            const pageHeight = this.tableData.length ? h : 260
            window.parent.postMessage({ height: pageHeight, type: 'serviceHeight' }, this.$fixIframeArr())
            this.$refs['table'][val.name - 1].doLayout()
          })
        })
      },
      // 确认
      async handleSend() {
        const obj = {
          prjId: this.urlParams.prjId,
          ifSingleProject: this.urlParams.ifSingleProject
        }
        const h = this.$createElement
        const { data } = await getSumMoneyIsNullNew({ ...obj, subPrjIds: this.urlParams.subPrjIds })
        if (data > 0) {
          this.dialogVisible2 = true
          return
        }
        const response = await saveDetailVerifyNew({ ...obj, projectPilots: this.urlParams.projectPilots })

      if (response.code !== '200') {
        return
      }
      window.parent.postMessage({ type: 'serviceClose' }, this.$fixIframeArr())
      this.$message.success(response.message)
    },
    async changeMsg(status) {
      this.dialogVisible2 = false
      if (status) {
        const obj = {
          prjId: this.urlParams.prjId,
          ifSingleProject: this.urlParams.ifSingleProject
        }
        const res = await deleteAllUselessDetailNew({ ...obj, projectPilots: this.urlParams.projectPilots })
        if (res.code !== '200') {
          return
        }
        this.$message.success('删除成功')
      }
    }
  }

}
</script>
<style lang="scss" scoped>
.tips {
  margin-bottom: 10px;
  color: #EB4B4B;
}
</style>
